﻿package plugins.mstandio.testing{
		
	/**
	 * ...
	 * @author mstandio
	 */
	
	import flash.display.Sprite;	
	import flash.display.Stage;
	import flash.display.StageScaleMode;
	import flash.text.TextField;	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.utils.getDefinitionByName;	
	
	
	import plugins.mstandio.UniversalMap.cameras.*;	
	import plugins.mstandio.UniversalMap.navigation.MapButton;
	
		
	public class Cam_tester extends Sprite {	
		
		private var pan:Number;
		private var tilt:Number;		
		private var zoom:Number;
		private var panShift:Number;
		
		private var camAlpha:Number;
		private var camColor:String;
		private var camSize:Number;
		
		private var cam1Type:String;		
		private var cam2Type:String;		
		
		private var txtPan:TextField;
		private var txtTilt:TextField;
		private var txtZoom:TextField;
		private var txtCamSize:TextField;
		
		private var cam1:Cam;					
		private var cam2:Cam;
		private var camDefault:Cam;
		
		private var b1:MapButton;
		private var b2:MapButton;
		private var b3:MapButton;
		
			
		
			// classes need to be mentioned
			private var c1:Cam_default;
			private var c2:Cam_default_short;
			private var c3:Cam_orb;
			private var c4:Cam_border;
			private var c5:Cam_border_short;
			private var c6:Cam_camera;
			
	
		public function Cam_tester():void {								
			this.addEventListener(Event.ADDED_TO_STAGE, stageReady, false, 0, true);									
			
		}				
		
		
		private function stageReady(e:Event):void {			
			this.removeEventListener(Event.ADDED_TO_STAGE, stageReady);									
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			this.pan = 0;			
			this.tilt = 0;						
			this.zoom = 10;			
			this.panShift = 0;
			
			this.cam1Type = "Cam_camera";
			this.cam2Type = "Cam_orb";
			
			this.camSize = 1;
			this.camColor = "0xFF0000";
			this.camAlpha = 100;
			
			this.txtPan = new TextField();
			this.txtTilt = new TextField();
			this.txtZoom = new TextField();						
			this.txtCamSize = new TextField();						
			this.addChild(this.txtPan);
			this.addChild(this.txtTilt);
			this.addChild(this.txtZoom);
			this.addChild(this.txtCamSize);
			this.txtTilt.y += 20;
			this.txtZoom.y += 40;
			this.txtCamSize.y += 60;			
			
			this.txtPan.text = "pan: " + this.pan;
			this.txtTilt.text = "tilt: " + this.tilt;
			this.txtZoom.text = "zoom: " + this.zoom;
			this.txtCamSize.text = "zoom: " + this.zoom;
			this.txtCamSize.text = "size: " + this.camSize;
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);									
			
			
			
			this.makeCams();
			
			
			
		}
		
		private function makeCams():void {
			
			while (this.numChildren) {
				this.removeChildAt(0);
			}
			
			this.addChild(this.txtPan);
			this.addChild(this.txtTilt);
			this.addChild(this.txtZoom);
			this.addChild(this.txtCamSize);
			
			
			this.b1 = new MapButton();
			this.b1.drawButton(MapButton.BUTTON_SHOWING, this.camSize);			
			this.b2 = new MapButton();
			this.b2.drawButton(MapButton.BUTTON_SHOWING, this.camSize);						
			this.b3 = new MapButton();
			this.b3.drawButton(MapButton.BUTTON_SHOWING, this.camSize);			
			
			
			this.addChild(b1);
			this.addChild(b2);
			this.addChild(b3);
			
			
			var cls1:Class = getDefinitionByName("plugins.mstandio.UniversalMap.cameras." + this.cam1Type) as Class;			
			
			try	{
				this.cam1 = new cls1(this.pan, this.tilt, this.zoom, this.camSize,this.panShift, this.camColor)
			}
			catch(e:Error){
				this.cam1 = new Cam_default(this.pan, this.tilt, this.zoom, this.camSize,this.panShift, this.camColor)
			}						
			
			var cls2:Class = getDefinitionByName("plugins.mstandio.UniversalMap.cameras." + this.cam2Type) as Class;
			
			try	{
				this.cam2 = new cls2(this.pan, this.tilt, this.zoom, this.camSize,this.panShift, this.camColor)
			}
			catch(e:Error){
				this.cam2 = new Cam_default(this.pan, this.tilt, this.zoom, this.camSize,this.panShift, this.camColor)
			}						
					
			
			this.camDefault = new Cam_default(this.pan, this.tilt, this.zoom, this.camSize,this.panShift, this.camColor)
			
			this.addChild(cam1);
			this.cam1.x = 150; 
			this.cam1.y = 50; 
			
			b1.x = this.cam1.x;
			b1.y = this.cam1.y;
			
			this.addChild(cam2);
			this.cam2.x = 300; 
			this.cam2.y = 50; 
			
			b2.x = this.cam2.x;
			b2.y = this.cam2.y;
			
			this.addChild(camDefault);
			this.camDefault.x = 150; 
			this.camDefault.y = 150; 
			
			b3.x = this.camDefault.x;
			b3.y = this.camDefault.y;
		}
		
		
		
		private function keyDown(e:KeyboardEvent):void {			
			switch(e.keyCode) {
				
				case 38: { // up arrow
					if(this.tilt < 90){
						this.tilt += 5;
						cam1.rotate(this.pan, this.tilt);
						cam2.rotate(this.pan, this.tilt);
						camDefault.rotate(this.pan, this.tilt);
					}
					
				}break;
				
				case 40: { // down arrow
					if(this.tilt > -90){
						this.tilt -= 5;
						cam1.rotate(this.pan, this.tilt);
						cam2.rotate(this.pan, this.tilt);
						camDefault.rotate(this.pan, this.tilt);
					}
					
				}break;	
				
				case 37: { // left arrow					
					this.pan -= 5;
					cam1.rotate(this.pan, this.tilt);
					cam2.rotate(this.pan, this.tilt);
					camDefault.rotate(this.pan, this.tilt);
				}break;
				
				case 39: { // right arrow					
						this.pan += 5;
						cam1.rotate(this.pan, this.tilt);					
						cam2.rotate(this.pan, this.tilt);					
						camDefault.rotate(this.pan, this.tilt);					
				}break; 
				
				case 190: { // . - zoom in
					if (this.zoom<15){
						this.zoom += 1;
						cam1.zoom(this.zoom);
						cam2.zoom(this.zoom);
						camDefault.zoom(this.zoom);
					}
				}break;					
				
				case 188: { // , - zoom out
					if (this.zoom>1){
						this.zoom -= 1;
						cam1.zoom(this.zoom);
						cam2.zoom(this.zoom);
						camDefault.zoom(this.zoom);
					}
				}break;				
				
				case 219: { // [ - size+
					if (this.camSize >= 0.5) {
						this.camSize -= 0.1;						
						this.makeCams();
					}
				}break;				
				
				case 221: { // [ - size-
					if (this.camSize <= 1.6 ) {
						this.camSize += 0.1;
						this.makeCams();
					}
				}break;								
				
			}
			this.txtPan.text = "pan: " + this.pan;
			this.txtTilt.text = "tilt: " + this.tilt;
			this.txtZoom.text = "zoom: " + this.zoom;
			this.txtCamSize.text = "zoom: " + this.zoom;
			this.txtCamSize.text = "size: " + this.camSize;
		}
	}
}
